#pragma once
#include "../core/base/basic_types.h"
#include <vector>

enum KFormat {eRGBA_8888,eRGBA_L8, eUnknow};

class KBitmapDataHolder 
{
public:
	virtual bool BeginEdit() = 0;
	virtual void EndEdit() = 0;
	virtual bool Allocate(int w, int h, KFormat format) = 0;
	virtual void* GetRowPtr(int row) = 0;
};

class KBitmap : public KBitmapDataHolder
{
public:
	class CreationDesc 
	{
	public:
		bool wantAlpha;
		bool forceGray;
		BYTE defaultAlpha;
		// if color key is used, the alpha of the pixels with the specified color will be set to default alpha
		bool useColorKey;
		KColor colorKey;
		
		CreationDesc();
	};
	
public:
	KBitmap();
	KBitmap(const char* fileName);
	KBitmap(int width, int height, KFormat format);
	~KBitmap();
	
	KFormat GetFormat() const;
	BYTE* GetPixelData();
	const BYTE* GetPixelData() const;
	int Width() const;
	int Height() const;
	bool IsValid() const;
	
	virtual bool BeginEdit() {return true;}
	virtual void EndEdit() {}
	virtual bool Allocate(int w, int h, KFormat format);
	virtual void* GetRowPtr(int row);

	static bool LoadFromFile(const char* fileName, KBitmapDataHolder* pHolder, const CreationDesc& createDesc);
	
private:
	// image data
	int 	    m_width;
	int 	    m_height;
	int			m_bpp;
	std::vector<BYTE>	m_pData;
	KFormat		m_format;
	bool		m_isValid;
};
